// src/store/apiSlice/patientApiSlice.ts
import { apiSlice } from "@store/apiSlice/index";

export const patientApiSlice = apiSlice
    .enhanceEndpoints({ addTagTypes: ["patients"] })
    .injectEndpoints({
        endpoints: (builder) => ({
            // 查询患者列表
            requestPatients: builder.query<PatientResponse, undefined>({
                query: () => ({ url: "/patient/mylist" }),
                providesTags: ["patients"],
            }),
            // 添加患者
            addPatient: builder.mutation<
                HealthResponse<{ id: string }>,
                Pick<Patient, "name" | "idCard" | "defaultFlag" | "gender">
                >({
                query: (body) => ({ url: "/patient/add", method: "POST", body }),
                invalidatesTags: ["patients"],
            }),
            modifyPatient: builder.mutation<
                HealthResponse<{ id: string }>,
                { id: string } & Pick<Patient, "name" | "idCard" | "defaultFlag" | "gender">
                >({
                query: (body) => ({ url: "/patient/update", method: "PUT", body }),
                invalidatesTags: ["patients"],
            }),
            //删除患者
            removePatient : builder.mutation<HealthResponse<{id:string}>,string>({
                query: (id) =>({ url: `/patient/del/${id}`,method:"DELETE"}),
                invalidatesTags : ["patients"]
            }),
            // 根据患者 id 获取患者信息
            requestPatient: builder.query<HealthResponse<Patient>, string>({
                query: (id) => ({ url: `/patient/info/${id}` }),
            }),
        }),
    });

export const { useRequestPatientsQuery,useAddPatientMutation,useModifyPatientMutation,useRemovePatientMutation,useRequestPatientQuery} = patientApiSlice;